package daoImpl;

import baseDao.BaseDao;
import dao.ContentDao;
import entity.Floor;
import entity.Post;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

/**
 * Created by miaoyu on 2017/7/14.
 */
public class ContentDaoImpl extends BaseDao implements ContentDao{
    private String getFloorContent(int floorID){
        FloorDaoImpl floorDao = new FloorDaoImpl();
        Floor floor = floorDao.queryFloorByID(floorID);
        System.out.println(floor.getContent());
        return floor.getContent();
    }
    private String getPostContent(int postID){
        PostDaoImpl postDao = new PostDaoImpl();
        Post post = postDao.queryPostByID(postID);
        return post.getContent();
    }

    public String getContent(String url) {
        StringBuffer sb = new StringBuffer();
        File file = new File(url);
        BufferedReader reader = null;
        if(!file.exists()){
            return null;
        }
        try {
            String read = "";
            reader = new BufferedReader(new FileReader(file));
            while((read = reader.readLine())!=null){
                sb.append(read);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return sb.toString();
    }

    public String queryFloorContent(int floorID) {
        System.out.println(getContent(getFloorContent(floorID)));
        return getContent(getFloorContent(floorID));
    }

    public String queryPostContent(int postID) {
        return getContent(getPostContent(postID));
    }
}
